package utils

// CollectMap 函数接受一个切片和一个函数作为参数，并返回一个新的切片
func CollectMap[I any, O any](input []I, f func(I) O) []O {
	result := make([]O, len(input))
	for i, v := range input {
		result[i] = f(v)
	}
	return result
}

func CollectToDict[K comparable, V any](input []V, f func(V) K) map[K]V {
	result := make(map[K]V, len(input))
	for _, v := range input {
		result[f(v)] = v
	}
	return result
}

func RemoveDuplicates[T comparable](slice []T) []T {
	uniqueMap := make(map[T]struct{})
	var uniqueSlice []T

	for _, value := range slice {
		if _, exists := uniqueMap[value]; !exists {
			uniqueMap[value] = struct{}{}
			uniqueSlice = append(uniqueSlice, value)
		}
	}

	return uniqueSlice
}
